<?php
/**
 * create to 2021/6/25 11:49
 */

namespace app\mainadmin\controller\Liveing;

use app\AdminController;
use app\distribution\model\DividendRoleModel;
use app\mainadmin\model\LiveUsersModel;

/**
 * 直播间用户管理
 * Class Index
 * @package app\store\controller
 */
class Users extends AdminController
{
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize(){
        parent::initialize();
        $this->Model = new LiveUsersModel();
    }
    /*------------------------------------------------------ */
    //-- 主页
    /*------------------------------------------------------ */
    public function index() {
        if (input("p/d")&&input("p/d")>0) {
            return $this->getList(false);
        }
        $this->getList(true);
        $this->assign("roleOpt", arrToSel($this->roleList, $this->search['roleId']));
        return $this->fetch();
    }

    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false) {

        $this->search['roleId'] = input('rode_id', -1);
        $this->search['nickname'] = input("nickname");
        $this->search['user_id'] = input("user_id", 0, 'intval');
        $this->search['status'] = input("status", 0, 'intval');

        if ($this->search['roleId'] == 'all_role'){
            $where[] = ' u.role_id > 0 ';
        }elseif ($this->search['roleId'] >= 0) {
            $where[] = ' u.role_id = ' . $this->search['roleId'] * 1;
        }
        if ($this->search['user_id']>0) {
            $where[] = " u.user_id like '%{$this->search['user_id']}%'";
        }
        if (!empty($this->search['nickname'])) {
            $where[] = " u.nick_name like '%{$this->search['nickname']}%'";
        }
        if ($this->search['status']==1) {
            $where[] = ' lg.status=1';
        }
        $viewObj = $this->Model->alias('lg')->join("users u", 'lg.user_id=u.user_id', 'left')->where(join(' AND ', $where))->field('lg.*,u.nick_name,u.role_id');
        $data = $this->getPageList($this->Model,$viewObj);
        $DividendRoleModel = new DividendRoleModel();
        $this->roleList = $DividendRoleModel->getRows();
        $this->assign("data", $data);
        $this->assign("roleList", $this->roleList);
        if ($runData == false){
            $data['content']= $this->fetch('mainadmin@liveing/users/list')->getContent();
            unset($data['list']);
            return $this->success('','',$data);
        }
        return true;
    }
    /*------------------------------------------------------ */
    //-- 拉入|拉出黑名单
    /*------------------------------------------------------ */
    public function explain(){
        $id = input('id',0,'intval');
        $status = input('status',0,'intval');
        if ($id < 1){
            return $this->error('传参错误.');
        }
        $row = $this->Model->where('id',$id)->find();
        if (empty($row)){
            return $this->error('没有找到相关记录.');
        }
        $save_data = ['status'=>$status];
        $res = $this->Model->where('id',$id)->update($save_data);
        if ($res < 1){
            return $this->error('操作失败.');
        }
        return $this->success('操作成功.');
    }
}